// pages/hoteldetail/hoteldetail.js

const app = getApp();

var count_click_tc = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hotel_id:'',
    ruzhu_time:'',
    leave_time:'',
    hotel_list_h:'300rpx',

    swiperCurrent: 0,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 800,
    circular: true,

    sh_detail: {},
    hotelroom_list:[],
    show_hide_text: '显示更多',
    
    hideHeader: true,
    hideBottom: true,
    refreshTime: '', // 刷新的时间 
    allPages: '', // 总页数
    currentPage: 1, // 当前页数  默认是1
    loadMoreData: '加载更多……',

    isHiddenMes1: true,
    // pinglun_num: 216,//评论数量
    // averageScore: 4.9,//平均分数
    // zan_num: 129,//点赞数量
    pinglun_list: [],

    hotel_day:1,//酒店停留时间

    showModal:false,

    screenData:[{//筛选
      styleName:'取消方式',
      isSelected:1,
       styleAry:[{
         id:1,
         text:'可取消',
       },{
          id: 2,
          text: '付费取消',
       }]
    },{
        styleName: '支付方式',
        isSelected: 1,
        styleAry: [{
          id: 1,
          text: '到店支付',
        }, {
          id: 2,
          text: '在线支付',
        }]
    },{
        styleName: '早餐',
        isSelected: 1,
        styleAry: [{
          id: 1,
          text: '含早',
        }, {
          id: 2,
          text: '单早',
        },{
          id: 3,
          text: '双早',
        }]
    },{
        styleName: '床型',
        isSelected: 1,
        styleAry: [{
          id: 1,
          text: '大床',
        }, {
          id: 2,
          text: '双床',
        }]
    }],
    screenData_reset:[],//重置用

    btnSelect:1,// 重置 or 确定

    cur_screen:[false,false,false,false],//含早，大床，到店付，可取消
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    var date = new Date();
    this.setData({
      refreshTime: date.toLocaleTimeString()
    })

    var time0 = app.util.getDateStr(0);
    time0 = time0.split('\n')[0];
    time0 = time0.replace('-','月');

    var time1 = app.util.getDateStr(1);
    time1 = time1.split('\n')[0];
    time1 = time1.replace('-', '月');

    console.log(time0, time1);
    
    that.setData({
      ruzhu_time: time0,
      leave_time: time1
    })
   
    //获取酒店的信息
    var hotel_id = parseInt(options.id);
    
    this.setData({
      hotel_id: hotel_id
    })
    this.hotelData(hotel_id);
    
    //房间列表
    this.hotel_roomList(hotel_id);
    
    //获取评论数据
    this.getData();

  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  /*
  * 酒店详情数据
  */
  hotelData:function(id){
    var that = this;
    app.apiPost(app.apiList.hotel_info, {
      content:{
        id:id
      }
    }, function (res) {
      console.log(res,'酒店详情');
      that.setData({
        sh_detail: res.content
      })
    })
  },
  /**
   * 酒店房间列表
   */
  hotel_roomList:function(id){
    var that = this;
    app.apiPost(app.apiList.hotelroom_list, {
      content: {
        hotelId:id,
        pageNum:1,
        pageSize:10
      }
    }, function (res) {
        console.log(res,'酒店房间');
        that.setData({
          hotelroom_list: res.content.list
        })
    })
  },
  /*
  *选择入住时间
  */
  ruzhu:function(){
   
    wx.navigateTo({
      url: '../dateselect/dateselect?type=ruzhu',
    })
  },
  /*
  *选择离店时间
  */
  leave:function(){
   
    wx.navigateTo({
      url: '../dateselect/dateselect?type=leave',
    })
  },
  /**
   *点击选择房间 
   */
  select_room:function(e){
    console.log(e.currentTarget.dataset.hotelid, e.currentTarget.dataset.id,'点击选择房间');

    var obj={};
    obj.hotelid = parseInt(e.currentTarget.dataset.hotelid);
    obj.id = parseInt(e.currentTarget.dataset.id);
    
    obj = JSON.stringify(obj);

    wx.navigateTo({
      url: '../roominfo/roominfo?obj=' + obj,
    })
  },
  /**
   * 含早。。。筛选
   */
  screen:function(e){
    console.log(e.currentTarget.dataset.index,'当前页筛选');
    var i = parseInt(e.currentTarget.dataset.index) - 1;

    var cur = !this.data.cur_screen[i];
    
    var obj = 'cur_screen['+i+']';
    this.setData({
      [obj]: cur
    })

  },
  /**
   * 查看所有评论
   */
  searchAllPL: function () {
    console.log('查看所有评论');
    var self = this;
    wx.navigateTo({
      url: '../allcomment/allcomment?siteId=' + self.data.hotel_id,
    })
  },
  /**
   * 我要评论
   */
  yaoPinglun: function () {
    console.log('我要评论');
    wx.navigateTo({
      url: '../publishcomment/publishcomment',
    })
  },
  /*
  *显示更多
  */
  show_hide: function () {
    //改变高度 改变文字
    count_click_tc++;
    var r = count_click_tc % 2;
    if (r == 0) {
      //合起状态
      this.setData({
        show_hide_text: '显示更多',
        hotel_list_h: '300rpx'
      })
    } else {
      //展开状态
      this.setData({
        show_hide_text: '收起',
        hotel_list_h: '400rpx'
      })
      //可以获取更多的酒店....
    }

  },
  
  /*
  * 酒店中，房间预定
  */
  book_hotel:function(e){
    console.log(e.currentTarget.dataset.index,'预定');
  },
  /*
  * 详细介绍
  */
  detail_introduce:function(){

    var that = this;
    wx.navigateTo({
      url: '../hotelintroduce/hotelintroduce?id=' + that.data.sh_detail.id,
    })

  },
  /**
   * 酒店设施
   */
  hotel_facilities:function(){
    
    var that = this;
    wx.navigateTo({
      url: '../hotelfacilities/hotelfacilities?id=' + that.data.sh_detail.id,
    })
  },
  /*
  *导航
  */
  searchDaohang:function(){

    var obj = {};
    obj.name = this.data.sh_detail.name;
    obj.lat = this.data.sh_detail.lat;
    obj.lng = this.data.sh_detail.lng;
    obj.address = this.data.sh_detail.address;

    obj = JSON.stringify(obj);

    wx.navigateTo({
      url: '../shopnavigate/shopnavigate?storeInfo=' + obj,
    })
  },
  /*
  *拨打电话
  */
  cellPhone:function(){
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.sh_detail.phone
    })
  },

  //轮播图的切换事件
  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  //点击指示点切换
  chuangEvent: function (e) {
    this.setData({
      swiperCurrent: e.currentTarget.id
    })
  },
  //点击图片触发事件
  swipclick: function (e) {
    console.log(this.data.swiperCurrent);
    wx.switchTab({
      url: this.data.links[this.data.swiperCurrent]
    })
  },
  hideModal:function(){

    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModal: false
      })
    }.bind(this), 200)
  },
  screenHotel:function(){

    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModal: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  // 上拉加载更多
  loadMore: function () {
    var self = this;
    // 当前页是最后一页
    if (self.data.currentPage == self.data.allPages) {
      self.setData({
        loadMoreData: '已经到顶'
      })
      return;
    }
    setTimeout(function () {
      console.log('上拉加载更多');
      var tempCurrentPage = self.data.currentPage;
      tempCurrentPage = tempCurrentPage + 1;
      self.setData({
        currentPage: tempCurrentPage,
        hideBottom: false
      })
      self.getData();
    }, 300);

  },
  // 下拉刷新
  refresh: function (e) {
    var self = this;
    setTimeout(function () {
      console.log('下拉刷新');
      var date = new Date();
      self.setData({
        currentPage: 1,
        refreshTime: date.toLocaleTimeString(),
        hideHeader: false
      })
      self.getData();
    }, 300);

  },
  /*
  *评论列表
  */
  getData: function () {
    var self = this;
    var pageIndex = self.data.currentPage;

    app.apiPost(app.apiList.meishi_comment_list, {
      content:{
        pageNum: pageIndex,
        pageSize:20,
        siteId: self.data.hotel_id
      }
     
    }, function (res) {
      console.log(res, '酒店-评论列表');

      var pinglun_list = res.content.pageData.list;

      if (pageIndex == 1) { // 下拉刷新
        self.setData({
          allPages: res.content.pageData.total,
          pinglun_list: pinglun_list,
          hideHeader: true
        })

      } else { // 加载更多
        var tempArray = self.data.pinglun_list;
        tempArray = tempArray.concat(pinglun_list);
        self.setData({
          allPages: res.content.pageData.total,
          pinglun_list: tempArray,
          hideBottom: true
        })
      }

      if (self.data.pinglun_list.length > 0) {
        self.setData({
          isHiddenMes1: false
        })
      }
    })

  },
  /**
   * 筛选-选项
   */
  selectStype:function(e){
   

    var parent_i = e.currentTarget.dataset.index;
    var child_i = e.currentTarget.dataset.i;
    console.log(e, '筛选选择', parent_i, child_i);

    var st = 'screenData[' + parent_i + '].isSelected';
    this.setData({
      [st]: child_i
    })
    
  },
  //重置
  reset_style:function(){

  },
  /**
   * 筛选后，确定
   */
  confirm_style:function(){
    var that = this;
     wx.navigateTo({
       url: '../hotelist/hotelist?id=' + that.data.hotel_id,
     })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})